package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.Organization;
import com.battle.pojo.entity.User;
import com.battle.pojo.entity.UserAttention;
import com.battle.pojo.param.userAttention.*;
import com.battle.pojo.vo.userAttention.*;
import com.battle.pojo.dto.userAttention.*;
import com.battle.service.repository.in.OrganizationRepository;
import com.battle.service.repository.in.UserAttentionRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.UserAttentionService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：用户关注服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class UserAttentionServiceImpl implements UserAttentionService {

    private final UserAttentionRepository userAttentionRepository;
    private final UserRepository userRepository;
    private final OrganizationRepository organizationRepository;

    public UserAttentionServiceImpl(UserAttentionRepository userAttentionRepository, UserRepository userRepository, OrganizationRepository organizationRepository) {
        this.userAttentionRepository = userAttentionRepository;
        this.userRepository = userRepository;
        this.organizationRepository = organizationRepository;
    }


    /**
     * 创建用户关注
     *
     * @param param
     */
    @Override
    public void createUserAttention(CreateUserAttentionParam param) {
        UserAttention userAttention = new UserAttention();
        BeanUtils.copyProperties(param, userAttention);
        Long id = IdGenerator.SNOW.generate();
        userAttention.setId(id);
        userAttentionRepository.create(userAttention);
    }

    /**
     * 修改用户关注
     *
     * @param param
     */
    @Override
    public void editUserAttention(EditUserAttentionParam param) {
        Long id = param.getId();
        UserAttention userAttention = userAttentionRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, userAttention);
        userAttentionRepository.edit(userAttention);
    }

    /**
     * 移除用户关注
     *
     * @param id
     */
    @Override
    public void removeUserAttention(Long id) {
        userAttentionRepository.removeById(id);
    }

    /**
     * 启动/禁用用户关注
     *
     * @param id
     */
    @Override
    public void enabledUserAttention(Long id) {
        UserAttention userAttention = userAttentionRepository.getByIdCheck(id);
        userAttention.setIsDisable(!userAttention.getIsDisable());
        userAttentionRepository.edit(userAttention);
    }

    /**
     * 获取用户关注详情
     *
     * @param id
     * @return
     */
    @Override
    public GetUserAttentionVo getUserAttention(Long id) {
        UserAttention userAttention = userAttentionRepository.getByIdCheck(id);
        GetUserAttentionVo vo = new GetUserAttentionVo();
        BeanUtils.copyProperties(userAttention, vo);
        return vo;
    }

    /**
     * 获取用户关注 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetUserAttentionListVo> getUserAttentionList(GetUserAttentionListParam param) {

        SelectUserAttentionDto dto = new SelectUserAttentionDto();
        BeanUtils.copyProperties(param, dto);
        Page<GetUserAttentionListVo> page = userAttentionRepository.selectUserAttention(dto);
        List<GetUserAttentionListVo> list =  page.getRecords();

        if (CollectionUtil.isNotEmpty(list)){
            List<Long> userIds = new ArrayList<>();
            List<Long> deptIds = new ArrayList<>();

            list.forEach(v -> {
                userIds.add(v.getIssueUserId());
                deptIds.add(v.getIssueDepartmentId());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);
            Map<Long, Organization> deptMap = organizationRepository.getMap(deptIds);

            for (GetUserAttentionListVo vo : list) {
                User operation = userMap.get(vo.getIssueUserId());
                if (operation != null) {
                    vo.setIssueName(operation.getName());
                }
                Organization dept = deptMap.get(vo.getIssueDepartmentId());
                if (dept != null) {
                    vo.setIssueOrganizationName(dept.getName());
                }
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取用户关注下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetUserAttentionDownListVo> getUserAttentionDownList(GetUserAttentionDownListParam param) {
        UserAttention po = new UserAttention();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<UserAttention> userAttentionList = userAttentionRepository.getListByEntity(po);

        List<GetUserAttentionDownListVo> list = new ArrayList<>();
        for (UserAttention userAttention : userAttentionList) {
            GetUserAttentionDownListVo vo = new GetUserAttentionDownListVo();
            BeanUtils.copyProperties(userAttention, vo);
            list.add(vo);
        }
        return list;
    }


}



